using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PatronReservedBooks : System.Web.UI.UserControl
{
    ManageReservedBook bookInfo;
    ReservedBook book;
    Patron patron;
    int rID;

    protected void Page_Load(object sender, EventArgs e)
    {
        patron = (Patron)Session["Patron"];
        bookInfo = new ManageReservedBook();
        book = new ReservedBook();
        displayPatronReservedBooks();
    }

    protected void displayPatronReservedBooks()
    {
        SqlDataAdapter data = bookInfo.getPatronBooks(patron.PersonID);
        DataSet d = new DataSet();
        data.Fill(d);

        gvPatronReservedBooks.DataSource = d;
        gvPatronReservedBooks.DataBind();

        if (gvPatronReservedBooks.Rows.Count < 1)
        {
            lblMessage.Text = "You have no books reserved.";
            gvPatronReservedBooks.Visible = false;
        }
        else
        {
            lblMessage.Text = "";
            gvPatronReservedBooks.Visible = true;
        }
    }
    protected void gvPatronReservedBooks_SelectedIndexChanged(object sender, EventArgs e)
    {
        rID = Convert.ToInt32(gvPatronReservedBooks.SelectedDataKey.Value.ToString());
        cancelBook(rID);
        //lblAlert.Visible = true;
        //btnNo.Visible = true;
        //btnYes.Visible = true;
    }

    protected void cancelBook(int rID)
    {
        try
        {
            book = bookInfo.getReservedBook(rID);
            book.ReserveStatus = "Canceled";
            bookInfo.updateReservation(book);
            displayPatronReservedBooks();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Cancellation failed. Please contact Librarian to cancel book reservation.";
        }

    }
    //protected void btnYes_Click(object sender, EventArgs e)
    //{
    //    lblAlert.Visible = false;
    //    btnYes.Visible = false;
    //    btnNo.Visible = false;
    //    cancelBook(rID);
    //}
    //protected void btnNo_Click(object sender, EventArgs e)
    //{
    //    lblAlert.Visible = false;
    //    btnYes.Visible = false;
    //    btnNo.Visible = false;
    //}
}
